package controller;

import entity.Person;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * PersonController
 *
 * @author byc
 * @date 2022-01-17
 */
public class PersonController {

    public static void main(String[] args) {
        //Test1
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "New York"));
        personList.add(new Person("Jack", 7000, 23, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 25, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 23, "female", "New York"));
        personList.add(new Person("Owen", 9500, 23, "male", "New York"));
        personList.add(new Person("Alisa", 7900, 23, "female", "New York"));

        List<String> fiterList = personList.stream().filter(x -> x.getSalary() > 8000).map(Person::getName)
                .collect(Collectors.toList());
        System.out.println("高于8000的员工姓名：" + fiterList);

        System.out.println("----------------------");

        //Test3 获取员工工资最高的的人
        Optional<Person> maxSalary = personList.stream().max(Comparator.comparingInt(Person::getSalary));
        System.out.println("员工工资最大值：" + maxSalary.get().getSalary());

        System.out.println("----------------------");

        //Test4将员工工资全都加1000
        List<Person> personListNew = personList.stream().map(person -> {
            person.setSalary(person.getSalary() + 10000);
            return person;
        }).collect(Collectors.toList());
        for (Person person : personListNew) {
            System.out.println(person.getName() + " 薪水:" + person.getSalary());
        }

        System.out.println("----------------------");

        //Test5员工工资求和
        Optional<Integer> sumSalary = personList.stream().map(Person::getSalary).reduce(Integer::sum);
        Optional<Integer> sumSalary1 = personList.stream().map(Person::getSalary).reduce((x,y) -> x + y);
        Integer sumSalary3 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(),Integer::sum);
        Integer sumSalary4 = personList.stream().reduce(0, (sum, p) -> sum += p.getSalary(),(sum1,sum2) -> sum1 + sum2);
        System.out.println("薪水总和:" + sumSalary.get());
        System.out.println("薪水总和:" + sumSalary1.get());


    }
}
